const express = require('express');
const socketIO = require('socket.io');
const http = require('http');
const path = require('path');

// http 和 socketIO 混合用

// express
const app = express();
const server = http.createServer(app);
app.use(express.static(path.resolve(__dirname, 'public')));

// websocket
const io = socketIO(server);

io.on('connection', (socket) => {
  // 当有一个新的客户端连接到服务器成功之后，触发的事件
  console.log('新的客户端连接进来了');
  socket.on('msg', (chunk) => {
    // 监听客户端的msg消息
    console.log(chunk.toString('utf-8'));
  });
  const timer = setInterval(function () {
    //每隔两秒钟，发送一个消息给客户端，消息为test
    // emit 可以自定义 消息格式
    socket.emit('test', 'test message from server');
  }, 2000);

  socket.on('disconnect', () => {
    clearInterval(timer);
    console.log('closed');
  });
});

// 监听端口
server.listen(5008, () => {
  console.log('server listening on 5008');
});

// <!DOCTYPE html>
// <html lang="en">
//   <head>
//     <meta charset="UTF-8" />
//     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
//     <title>Document</title>
//   </head>
//   <body>
//     <button>发送数据到服务器</button>
//     <script src="https://cdn.bootcdn.net/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
//     <script>
//       const socket = io.connect();
//       document.querySelector("button").onclick = function () {
//         socket.emit("msg", "msg from client");
//       };

//       socket.on("test", (chunk) => {
// 监听 test 的消息
//         console.log(chunk);
//       });

//       socket.on("disconnect", () => {
//         console.log("closed");
//       });
//     </script>
//   </body>
// </html>
